package com.forxindu.kit.box;

import android.text.Html;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * Created by dongbinbin on 2015/9/9.
 */
public class ParserKit {
    /**
     * 解析的开始key值
     */
    public static final String START = "start";
    /**
     * 解析的结束key值
     */
    public static final String END = "end";
    /**
     * img标签key值
     */
    public static final String IMG = "img";
    //定义正则表达式
    public static final String P = "<p>.+?</p>";
    public static final String AT = "@.+?\n</a>";
    public static final String LINKS = "网页链接</a>";
    public static final String URL = "href='http://.+?'>";

    public static String getPostContentString(String source) {
        String parserContent = "";
        Pattern pPattern = Pattern.compile(P);
        Matcher pMatcher = pPattern.matcher(source);
        HashMap<String, String> pMap = new HashMap<String, String>();
        while (pMatcher.find()) {
            pMap.put(START, pMatcher.start() + "");
            pMap.put(END, pMatcher.end() + "");
        }
        if (pMap != null) {
            if (pMap.get(START) != null && !pMap.get(START).equals("") && pMap.get(END) != null && !pMap.get(END).equals("")) {
                parserContent = Html.fromHtml(source.substring(Integer.parseInt(pMap.get(START)), Integer.parseInt(pMap.get(END)))).toString();
//                parserContent = source.substring(Integer.parseInt(pMap.get(START)), Integer.parseInt(pMap.get(END)));
            }
        }
        parserContent = Html.fromHtml(source).toString();
//        source = source.replace(" ", " \n");
        parserContent = parserContent.replace("￼", "");
//        parserContent = parserContent.replace(" ", " \n");
        Pattern atPattern = Pattern.compile(AT);
        Matcher atMatcher = atPattern.matcher(source);
        List<HashMap<String, String>> atMapList = new ArrayList<HashMap<String, String>>();
        while (atMatcher.find()) {
            HashMap<String, String> map = new HashMap<String, String>();
            map.put(START, atMatcher.start() + "");
            map.put(END, atMatcher.end() + "");
            atMapList.add(map);
        }
        List<String> atStrList = new ArrayList<String>();
        for (HashMap<String, String> map : atMapList) {
            if (map != null) {
                if (map.get(START) != null && !map.get(START).equals("") && map.get(END) != null && !map.get(END).equals("")) {
//                    String atName = Html.fromHtml(source.substring(Integer.parseInt(map.get(START)), Integer.parseInt(map.get(END)))).toString();
                    String atName = source.substring(Integer.parseInt(map.get(START)), Integer.parseInt(map.get(END)) - 4);
                    if (!atStrList.contains(atName)) {
                        atStrList.add(atName);
                    }
                }
            }
        }
        for (String userName : atStrList) {
            if (!userName.contains(" ")) {
                parserContent = parserContent.replace(userName, Html.fromHtml(userName).toString() + " ");
            } else {
                parserContent = parserContent.replace(userName, Html.fromHtml(userName).toString().trim() + " ");
            }
        }
        Pattern linksPattern = Pattern.compile(LINKS);
        Matcher linksMatcher = linksPattern.matcher(source);
        List<HashMap<String, String>> linksMapList = new ArrayList<HashMap<String, String>>();
        while (linksMatcher.find()) {
            HashMap<String, String> map = new HashMap<String, String>();
            map.put(START, linksMatcher.start() + "");
            map.put(END, linksMatcher.end() + "");
            linksMapList.add(map);
        }
        List<String> linksStrList = new ArrayList<String>();
        for (HashMap<String, String> map : linksMapList) {
            if (map != null) {
                if (map.get(START) != null && !map.get(START).equals("") && map.get(END) != null && !map.get(END).equals("")) {
                    linksStrList.add(Html.fromHtml(source.substring(Integer.parseInt(map.get(START)), Integer.parseInt(map.get(END)))).toString());
                }
            }
        }
        if (linksStrList != null && linksStrList.size() > 0) {
            parserContent = parserContent.replace(linksStrList.get(0), "" + linksStrList.get(0));
        }
        return parserContent.trim();
    }

    public static List<String> getPostContentLinksList(String source) {
        List<String> linksList = new ArrayList<String>();
        Document doc = Jsoup.parse(source);
        Elements links = doc.select("a");
        for (Element link : links) {
            String linkHref = link.attr("href");
            if (!JavaKit.isStringEmpty(linkHref)) {
                linksList.add(linkHref);
            }
        }
        return linksList;
    }

    public static List<String> getPostContentImgList(String source) {
        List<String> imagesList = new ArrayList<String>();
        Document doc = Jsoup.parse(source);
        Elements images = doc.select("img");
        for (Element image : images) {
            String imageUrl = image.attr("src");
            if (!JavaKit.isStringEmpty(imageUrl)) {
                imagesList.add(imageUrl);
            }
        }
        return imagesList;
    }

    public static List<String> getAtList(String source) {
        List<String> atsList = new ArrayList<String>();
        Document doc = Jsoup.parse(source);
        Elements ats = doc.select("a");
        for (Element at : ats) {
            String atName = at.text();
            if (!JavaKit.isStringEmpty(atName) && String.valueOf(atName.charAt(0)).equals("@")) {
                atsList.add(atName);
            }
        }
        return atsList;
    }

    public static String getParserContent(List<String> linksList, String postContent) {
        String parserContent = postContent;
        if (!JavaKit.isListEmpty(linksList)) {
            for (String url : linksList) {
                if (!JavaKit.isStringEmpty(url)) {
                    parserContent = parserContent.replace(url, " 网页链接");
                }
            }
        }
        return parserContent.trim();
    }
}
